﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ElectronicsMarket_Desktop.BL;

namespace ElectronicsMarket_Desktop
{
    public partial class frmDonHang : Form
    {
        long ma;
        OrderDetailsBL orderdetaitbl;
        OrdersBL orderbl;
        ProductBL productbl;
        CustomerBL customerbl;
        ReceiptBL receiptbl;
        List< ReceptDetailBL> lstreceiptdetailbl;
        ReceptDetailBL receiptdetail;
        int vitri;
        public frmDonHang()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (cbotrangthai.SelectedIndex == 1)
            {
                //
               
                orderbl = new OrdersBL();
                //
                orderbl.Order.ORDERS_ID = Convert.ToInt32(grddonhang.Rows[vitri].Cells[0].Value);
                orderbl.updateOrder();
                dataBind();
                //
                receiptbl.InsertReceipt();
                //
                for (int i = 0; i < lstreceiptdetailbl.Count; i++)
                {
                    lstreceiptdetailbl[i].InsertReceiptDeatial();
                }
                  
            }
        }

        private void fromDonHang_Load(object sender, EventArgs e)
        {
            dataBind();
           
        }

        private void grddonhang_SelectionChanged(object sender, EventArgs e)
        {
            int tongsoluong = 0;
            decimal tongtien = 0;
            orderdetaitbl = new OrderDetailsBL();
            productbl = new ProductBL();
            customerbl = new CustomerBL();
            receiptbl = new ReceiptBL();
            lstreceiptdetailbl = new List<ReceptDetailBL>();
            //
            if (grddonhang.CurrentRow.Cells["coldathanhtoan"].Value.ToString() == "False")
                cbotrangthai.Enabled = true;
            else
                cbotrangthai.Enabled = false;

            vitri = grddonhang.CurrentRow.Index;
            //
           // receiptbl.Receipt.RECEIPT_ID = 1000 + Convert.ToInt32(grddonhang.CurrentRow.Cells[0].Value);
            ma= receiptbl.getLastPreceiptId() + 1;
            receiptbl.Receipt.RECEIPT_ID = receiptbl.getLastPreceiptId() + 1;
            receiptbl.Receipt.RECEIPT_DATE = DateTime.Now;
            receiptbl.Receipt.RECEIPT_TYPE = true;
            receiptbl.Receipt.RECEIPT_STAFF_ID = StaticParameter.Idnhanvien;

            //
            orderdetaitbl.Orderdetail.ORDERS_ID = Convert.ToInt32( grddonhang.CurrentRow.Cells[0].Value);
            grdchitietdonhang.AutoGenerateColumns = false;
            grdchitietdonhang.DataSource = orderdetaitbl.getOrderDetailByID();

            for (int i = 0; i < grdchitietdonhang.RowCount; i++)
            {
                receiptdetail = new ReceptDetailBL();
                tongsoluong = tongsoluong + Convert.ToInt32( grdchitietdonhang.Rows[i].Cells["colsoluong"].Value);
                tongtien = tongtien + Convert.ToDecimal(grdchitietdonhang.Rows[i].Cells["colthanhtien"].Value);
               // receiptdetail.Receipdetail.RECEIPT_ID = 1000 + Convert.ToInt32(grdchitietdonhang.Rows[i].Cells[0].Value);
                receiptdetail.Receipdetail.RECEIPT_ID = ma;
                receiptdetail.Receipdetail.RECEIPT_PRODUCT_ID=Convert.ToInt32(grdchitietdonhang.Rows[i].Cells["colmasanpham"].Value);
                receiptdetail.Receipdetail.AMOUNT=Convert.ToInt32(grdchitietdonhang.Rows[i].Cells["colsoluong"].Value);
                productbl.Product.PRODUCT_ID=receiptdetail.Receipdetail.RECEIPT_PRODUCT_ID;
                productbl.getProdcutByID();
                receiptdetail.Receipdetail.UNIT_TOTAL=receiptdetail.Receipdetail.AMOUNT*productbl.Product.PRODUCT_PRICE;
                lstreceiptdetailbl.Add(receiptdetail);
            }
            txtsoluongdat.Text = tongsoluong.ToString();
            txttongtien.Text = tongtien.ToString();
            //
            customerbl.Customer.CUSTOMER_ID = Convert.ToInt32(grddonhang.CurrentRow.Cells["colidkhachhang"].Value);
            customerbl.getCustomerByID();
            txtkhachhang.Text = customerbl.Customer.CUSTOMER_FISRT_NAME.ToString();
            txtemail.Text = customerbl.Customer.CUSTOMER_EMAIL.ToString();
            



        }
        void dataBind()
        {
            orderbl = new OrdersBL();
            grddonhang.AutoGenerateColumns = false;
            grddonhang.DataSource = orderbl.getAllOrder();

        }
    }
}
